/* ***********************
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is setTargetAttributes.js module.
 *
 * The Initial Developer of the Original Code is Rob Gerns.
 *
 * Portions created by the Initial Developer are Copyright (C) 2011
 * the Initial Developer. All Rights Reserved.
 *
 **************************/
function setTargetAttributes(){
    /****************************************************
    * Function Name: setTargetAttributes
    *
    * Purpose: Add a target of "_blank" to any links in the HTML.
    * This way if the user clicks on a link, the link
    * should open in their default web browser and not 
    * inside the OisterReader application window. If the user-agent
    * is Android, then the setMobileTargetAttributes function
    * is called instead.
    *
    *****************************************************/  
    var intCounter = 0;
    var objLinks = document.getElementsByTagName("a");
    var strUserAgent = navigator.userAgent;
    var strAndroid = strUserAgent.indexOf("Android") > -1;
    
    if (!strAndroid) {
        for (intCounter = 0; intCounter < objLinks.length; intCounter++) {
             objLinks[intCounter].setAttribute("target","_blank");
        }
    }
    else {
        setMobileTargetAttributes();
    }
}
function setMobileTargetAttributes() {
   /****************************************************
    * Function Name(s): setMobileTargetAttributes
    *
    * Purpose: This should only be called if the user-agent
    * is an Android browser/device.  This particular function
    * adds the necessary onclick code to every link in order
    * to use the Titanium Developer (appcelerator.com) API
    * to open any links in the device's web browser. This 
    * will allow the user much more control than would be 
    * possible if links were viewed within the Oister Reader 
    * app.
    *
    * (You should disable this function and provide an alternative
    *  if you are compiling your own version with something other
    *  than Titanium Developer)
    *****************************************************/ 

    var intCounter = 0;
    var objLinks = document.getElementsByTagName("a");
    var strLinks = "";
    var strTiEvent = "Ti.App.fireEvent('openURL', {url: '";
    var strOnclickAttr = "";
    var strHrefAttr = "href";
    
        for (intCounter = 0; intCounter < objLinks.length; intCounter++) {
        strLinks = objLinks[intCounter].innerHTML;
        strOnclickAttr = strTiEvent + strLinks + "'});";
        objLinks[intCounter].setAttribute("onclick", strOnclickAttr);
        objLinks[intCounter].removeAttribute(strHrefAttr);
        }
}
